<template>
  <el-dialog v-model="visible" :title="title">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="8em">
      <el-form-item label="订单编号" prop="code">
        <el-input v-model="form.code" placeholder="输入订单编号" />
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-select v-model="form.supplier" placeholder="请选择供应商">
          <el-option
            v-for="item in supplierStore.list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商户" prop="merchant">
        <el-select v-model="form.merchant" placeholder="请选择商户">
          <el-option
            v-for="item in merchantStore.list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账户余额" prop="amount">
        <el-input v-model="form.amount" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio value="freeze">冻结</el-radio>
          <el-radio value="normal">待结算</el-radio>
          <el-radio value="completed">已结算</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否有效" prop="is_active">
        <el-switch v-model="form.is_active" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button
          type="primary"
          :loading="supplierStore.loading.create || supplierStore.loading.update"
          @click="handleConfirm"
        >
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { defineModel } from "vue";
import { useSupplierStoreHook } from "@/store/modules/supplier/supplier";
import { useSupplierAccountStoreHook } from "@/store/modules/supplier/account";
import { useMerchantStoreHook } from "@/store/modules/merchant/merchant";
import { SupplierAccountForm } from "@/api/supplier/account";
import { cloneDeep } from "lodash-es";

const supplierAccountStore = useSupplierAccountStoreHook();

const supplierStore = useSupplierStoreHook();

const merchantStore = useMerchantStoreHook();

export type FormType = "add" | "edit";

export interface Props {
  type: FormType;
  form?: SupplierAccountForm;
}

const visible = defineModel({ type: Boolean, default: false });

const emits = defineEmits(["confirm"]);

const props = defineProps<Props>();

const formRef = ref();

const selectCityIds = ref<number[]>([]);

const defaultForm: SupplierAccountForm = {
  id: undefined,
  merchant: undefined,
  supplier: undefined,
  code: "",
  is_active: true,
  amount: "",
  status: "freeze",
};

const form = reactive<Partial<SupplierAccountForm>>(cloneDeep(defaultForm));

const rules = {
  code: [{ required: true, message: "请输入订单编号", trigger: "blur" }],
  supplier: [{ required: true, message: "请选择供应商", trigger: "blur" }],
  merchant: [{ required: true, message: "请选择商户", trigger: "blur" }],
  amount: [{ required: true, message: "请输入账户余额", trigger: "blur" }],
};

const title = computed(() => {
  return props.type === "add" ? "新增" : "编辑";
});

watch(visible, async (val) => {
  if (val) {
    await supplierStore.fetchList({ page: 1, size: 9999 });
    await merchantStore.fetchMerchantList({ page: 1, size: 9999 });
    if (props.type === "edit") {
      Object.assign(form, props.form);
    }
  } else {
    Object.assign(form, cloneDeep(defaultForm));
    formRef.value.resetFields();
    selectCityIds.value = [];
  }
});

function handleConfirm() {
  formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      if (props.type === "add") {
        await supplierAccountStore.fetchCreate(form);
      } else {
        await supplierAccountStore.fetchUpdate(form);
      }
      visible.value = false;
      emits("confirm");
    }
  });
}
</script>

<style scoped lang="scss"></style>
